/**
 * AjaxSteaming.js
 * 
 * Wayne Wan(wanyi@yihaodian.com)
 * 1:42:03 PM Feb 27, 2012 2012
 */
function aaaaa(respText){
	jQuery("#cnt").html(respText);
}

jQuery.qcomet = {
	/**
	 * 
	 * parameters for statusUpdatedCallback :  statusUpdatedCallback(xdr.responseText)
	 * @param action  -  command type : subscription|unsubscription
	 * @param eventName  -  the event that will be subscribed/unsubscribed
	 * @param payload  -  the payload of this request. (Optional, can be set to blank string)
	 * @param statusUpdatedCallback  -  status updated callback function.
	 */
	sendCommand : function(action, eventName , payload, statusUpdatedCallback){
		var requestFun ;
		if($.browser.mozilla){
			requestFun = this.doXHR;
		}else if($.browser.msie && parseInt($.browser.version)>7){
			requestFun = this.doXDR;
		}
		/*
		 * does not support streaming message
		 * so we can only use ajax polling method
		 */
		else{
			alert("your browser does not support streaming message!");
			return;
		}
		var paramValues = action + "|" + eventName + "|" + payload;
		var clientId = CoookieUtils.get("Q-Comet-ClientId");
		if(clientId==null){
			jQuery.ajax("clientTrackServlet?" + Math.random(), 
			{
				success : function(){
					requestFun(paramValues, statusUpdatedCallback);
				}
			});
		}else{
			requestFun(paramValues, statusUpdatedCallback);
		}
	},
	doXHR : function (paramValues, statusUpdatedCallback){
		if (!window.XMLHttpRequest)
		{
			alert("Your browser does not support the native XMLHttpRequest object.");
			return;
		}
		try{
			var xhr = new XMLHttpRequest();  
			var clientId = CoookieUtils.get("Q-Comet-ClientId");
			paramValues = clientId + "|" + paramValues;
			xhr.onload = function() {};
			xhr.onerror = function() {
				xhr.onerror = function(){alert("error : " + paramValues)}
				if(xhr.status == 408){
					// request timeout
					jQuery.qcomet.doXHR(paramValues, statusUpdatedCallback);
				}
				alert("Fatal Error."); 
			};
			xhr.onprogress = function() {
				try{
					statusUpdatedCallback(xhr.responseText); 
				}
				catch (e){
					alert("Exception: " + e);
				}
			 };
			xhr.open("GET", "MsgPushServlet?command=" + paramValues + "&" + Math.random(), true);
			xhr.send();
		}
		catch (e){
			alert("Exception: " + e );
		}
	},
	doXDR : function (paramValues, statusUpdatedCallback){
		if (!window.XDomainRequest)
		{
			alert("Your browser does not support the XDomainRequest object.");
			return;
		}
		try{
			var xdr = new XDomainRequest(); 
			if(xdr){
				var clientId = CoookieUtils.get("Q-Comet-ClientId");
				paramValues = clientId + "|" + paramValues;
				xdr.open("GET", "MsgPushServlet?command=" + paramValues + "&" + Math.random());
				xdr.onload = function() {};
				xdr.onerror = function(){alert("error : " + paramValues)}
				xdr.ontimeout = function(){
					// request timeout
					jQuery.qcomet.doXDR(paramValues, statusUpdatedCallback);
				}
				xdr.onprogress = function() {
					statusUpdatedCallback(xdr.responseText);
				};
				xdr.send();
			}else{
				alert("failed to create!");
			}
		}
		catch (e){
			alert("Exception: " + e );
		}
	}
}
